package com.ruoyi.project.poem.poemsetdetail.controller;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.poem.poemset.service.IPoemSetService;
import com.ruoyi.project.poem.poemsetdetail.domain.PoemSetDetail;
import com.ruoyi.project.poem.poemsetdetail.service.IPoemSetDetailService;

/**
 * 诗单详情Controller
 * 
 * @author shixiangwen
 * @date 2020-03-30
 */
@Controller
@RequestMapping("/poem/poemsetdetail")
public class PoemSetDetailController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger("sys-user");

	private String prefix = "poem/poemsetdetail";

	@Autowired
	private IPoemSetDetailService poemSetDetailService;

	@Autowired
	private IPoemSetService poemSetService;

	@RequiresPermissions("poem:poemsetdetail:view")
	@GetMapping("/{setId}")
	public String poemsetdetail(@PathVariable("setId") Long setId, ModelMap mmap) {
		mmap.put("poemSet", poemSetService.selectPoemSetById(setId));
		return prefix + "/poemsetdetail";
	}

	/**
	 * 查询诗单详情列表
	 */
	@RequiresPermissions("poem:poemsetdetail:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PoemSetDetail poemSetDetail) {
		startPage();
		List<PoemSetDetail> list = poemSetDetailService.selectPoemSetDetailList(poemSetDetail);
		return getDataTable(list);
	}

	/**
	 * 导出诗单详情列表
	 */
	@RequiresPermissions("poem:poemsetdetail:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(PoemSetDetail poemSetDetail) {
		List<PoemSetDetail> list = poemSetDetailService.selectPoemSetDetailList(poemSetDetail);
		ExcelUtil<PoemSetDetail> util = new ExcelUtil<PoemSetDetail>(PoemSetDetail.class);
		return util.exportExcel(list, "poemsetdetail");
	}

	/**
	 * 新增诗单详情
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增诗单详情
	 */
	@GetMapping("/importPoem/{setId}")
	public String importPoem(@PathVariable("setId") Long setId, ModelMap mmap) {
		mmap.addAttribute("setId", setId);
		return prefix + "/importPoem";
	}

	@RequiresPermissions("poem:poemsetdetail:add")
	@Log(title = "诗单详情", businessType = BusinessType.INSERT)
	@PostMapping("/saveImportPoem")
	@ResponseBody
	public AjaxResult saveImportPoem(Long setId, String poemIds) {

		String loginName = ShiroUtils.getLoginName();
		Date now = DateUtils.getNowDate();

		int sort = poemSetDetailService.selectMaxSort(setId);
		Long[] ids = Convert.toLongArray(poemIds);
		for (Long id : ids) {
			PoemSetDetail query = new PoemSetDetail();
			query.setSetId(setId);
			query.setPoemId(id);
			List<PoemSetDetail> list = poemSetDetailService.selectPoemSetDetailList(query);
			if (!list.isEmpty()) {
				continue;
			}
			PoemSetDetail detail = new PoemSetDetail();
			detail.setSetId(setId);
			detail.setPoemId(id);
			detail.setCreateBy(loginName);
			detail.setCreateTime(now);
			detail.setSort(++sort);
			poemSetDetailService.insertPoemSetDetail(detail);
		}

		return toAjax(true);
	}

	/**
	 * 新增保存诗单详情
	 */
	@RequiresPermissions("poem:poemsetdetail:add")
	@Log(title = "诗单详情", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PoemSetDetail poemSetDetail) {
		return toAjax(poemSetDetailService.insertPoemSetDetail(poemSetDetail));
	}

	/**
	 * 修改诗单详情
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		PoemSetDetail poemSetDetail = poemSetDetailService.selectPoemSetDetailById(id);
		mmap.put("poemSetDetail", poemSetDetail);
		return prefix + "/edit";
	}

	/**
	 * 修改保存诗单详情
	 */
	@RequiresPermissions("poem:poemsetdetail:edit")
	@Log(title = "诗单详情", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PoemSetDetail poemSetDetail) {
		return toAjax(poemSetDetailService.updatePoemSetDetail(poemSetDetail));
	}

	/**
	 * 删除诗单详情
	 */
	@RequiresPermissions("poem:poemsetdetail:remove")
	@Log(title = "诗单详情", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(poemSetDetailService.deletePoemSetDetailByIds(ids));
	}
}
